package cn.yougou.controller;

import cn.yougou.common.DtoUtil;
import cn.yougou.common.VoDataUtil;
import cn.yougou.dto.Dto;
import cn.yougou.service.YgUserCouponsService;
import cn.yougou.service.user.YgOrderService;
import cn.yougou.service.user.YgUserService;
import cn.yougou.vo.YgOrderVo;
import cn.yougou.vo.YgPaymentVo;
import cn.yougou.vo.YgResOrderVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单
 */
@RestController
@RequestMapping("ygOrder")
public class YgOrderController {
    @Reference
    private YgUserCouponsService ygUserCouponsService;
    @Reference
    private YgOrderService ygOrderService;
    @Autowired
    private VoDataUtil voDataUtil;

    /**
     * 查询图片and状态:根据用户
     */
    @PostMapping("/selorder")
    public Dto selorder(@RequestBody YgOrderVo ygOrderVo){
        if (ygOrderVo.getUserid() != null){
            if(ygOrderVo.getOrderstatus() == 0 || ygOrderVo.getOrderstatus() == null){
                ygOrderVo.setOrderstatus(null);
                return DtoUtil.returnDataSuccess(ygOrderService.selCondition(ygOrderVo));
            }else {
                //当有状态时进行查询
                return DtoUtil.returnDataSuccess(ygOrderService.selCondition(ygOrderVo));
            }
        }else {
            return DtoUtil.returnSuccess("系统错误:无用户",5001);
        }

    }

    /**
     * 删除订单
     */
    @PostMapping("delete")
    public int delete(@RequestBody YgOrderVo ygOrderVo){
        return ygOrderService.deleteById(ygOrderVo);
    }

    @PostMapping("/updateOrder")
    public Dto updateOrder(@RequestBody YgOrderVo ygOrderVo){
        return DtoUtil.returnDataSuccess(ygOrderService.updateOrder(ygOrderVo));
    }

    /**
     *新增订单
     * @param ygResOrderVo
     * @return
     */
    //@RequestBody YgResOrderVo ygResOrderVo
    @PostMapping("createOrder")
    public Dto createOrder(@RequestBody YgResOrderVo ygResOrderVo){
        ygResOrderVo.setYgResCommodityVos(voDataUtil.YgCartTransformYgResCommodityVo(ygResOrderVo.getYgCarts())) ;
        return ygOrderService.createOrder(ygResOrderVo);
    }

    /***
     * 用户支付
     * 订单数组
     * 用户id
     * 支付方式
     * 优惠券id
     * */
    @PostMapping("payment")
    public Dto payment(@RequestBody YgPaymentVo ygPaymentVo){
        boolean result = false;
        //支付方式 3余额 2微信 1支付宝
        if(ygPaymentVo.getModeOfPayment() == 3){
            result = ygOrderService.balancePayment(ygPaymentVo);
        }else if(ygPaymentVo.getModeOfPayment() == 2){
            result = ygOrderService.weixin(ygPaymentVo);
        }else if(ygPaymentVo.getModeOfPayment() == 1){
            result = ygOrderService.zhifubao(ygPaymentVo);
        }
        //判断是哪种支付方式
        if(!result){
            return DtoUtil.returnFail("支付失败","50001");
        }
        return DtoUtil.returnSuccess("200");
    }


   /* @GetMapping("wori")
    public Dto wori(){
        YgPaymentVo ygPaymentVo = new YgPaymentVo();
        ygPaymentVo.setUserId(1);
        ygPaymentVo.setModeOfPayment(0);
        Integer [] data = new Integer[]{79,80,81,82};
        ygPaymentVo.setCouponsId(94);
        ygPaymentVo.setOrderIdArr(data);
        YgResOrderVo orderYgResOrderVo = ygUserCouponsService.getOrderYgResOrderVo(ygPaymentVo);
        return DtoUtil.returnDataSuccess(orderYgResOrderVo);
    }*/
}